<?php

ob_start();
include_once "../connect.php";
// รับค่ามาเก็บไว้ใน 
$alt_Id = $_POST["alt_Id"];
$de_id = $_POST["id"];
$cn_id = $_POST["cn_id"];
$alt_Program = $_POST["chkProgram"];
$alt_Device = $_POST["chkDevice"];
$alt_Error = $_POST["txtError"];
$emp_id = $_POST["txtEmpid"];
$txtName = $_POST["txtNameEng"];
$mail = $_POST["mail"];

if (empty($alt_Id)) {
    $usermailServer = "itservice@adinop.co.th";
$passmailServer = "adinop123*";
include ("class.phpmailer.php");
function script_sendmail($to_name, $to_email, $from_name, $email_user_send, $email_pass_send, $subject, $body_html, $body_text) {

    $mail = new PHPMailer();
    $mail->From = $email_user_send;
    $mail->FromName = $from_name;

    $mail->AddAddress($to_email, $to_name);
    $mail->Subject = $subject;
    $mail->Body = $body_html;
    $mail->AltBody = $body_text;
    $mail->IsHTML(true);
    $mail->CharSet = "utf-8"; //กำหนด charset ของภาษาในเมล์ให้ถูกต้อง เช่น tis-620 หรือ utf-8

    $mail->IsSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
 //  $mail->Host = 'smtp.gmail.com';
  //  $mail->Port = 465;
    $mail->SMTPAuth   = true;
//$mail->SMTPDebug  = true;
    $mail->Username = $email_user_send;
    $mail->Password = $email_pass_send;

    $mail->Send();
    $mail->ClearAddresses();
}
	   
$to_email_it	="suchart.nan@adinop.co.th";
$from_name_it	="$mail";
$email_user_send_it	="$usermailServer";
$email_pass_send_it	="$passmailServer";;
$subject_it	="$txtName";
$body_html_it	="
<table width=\"550\" border=\"0\" cellspacing=\"1\" cellpadding=\"5\">
  <tr>
    <td colspan=\"2\">
<p style=\"font-size:16px; color:#660099\">แจ้งซ่อมคอมพิวเตอร์ เรื่อง $alt_Error  <br />เพิ่มเติม  <a href=\"http://webportal/systemit/\">&nbsp;webportal/systemit</a></p>
 </td>
  </tr>
    <tr>
    <td colspan=\"2\"><div>
      <p>Regards,       </p>
      <p>IT Service Team.</p>
      <p>http://webportal/systemit/</p>
    </div></td>
  </tr>
</table>
";
//$body_text ="";

script_sendmail($to_name,$to_email_it,$from_name_it,$email_user_send_it,$email_pass_send_it,$subject_it,$body_html_it,$body_text);
if(script_sendmail){
  echo "SEND MAIL COMPLETE";
}
    if(empty($alt_Id)){
    $newNum = mysql_result(mysql_query(" SELECT Max( alt_Num )+1 AS MaxID FROM altcomservice "),0,"MaxID"); 
    mysql_query($newNum);
    if ($newNum == NULL) { // ถ้าได้เป็นค่าว่าง หรือ null ก็แสดงว่ายังไม่มีข้อมูลในฐานข้อมูล
        $newNum = "1";
    } else {
        mysql_query($newNum); //ถ้าไม่ใช่ค่าว่าง
    }

    
    $sql = "INSERT INTO `db_itmanager`.`altcomservice` (
`de_id` ,
`alt_Date` ,
`alt_Time` ,
`cn_id` ,
`alt_Num` ,
`alt_Program` ,
`alt_Device` ,
`alt_Error` ,
`alt_status` ,
`id_member` ,
`emp_id` ,
`alt_create`

)
VALUES (
 '$de_id', NOW( ), NOW( ),'$cn_id', '$newNum', '$alt_Program', '$alt_Device', '$alt_Error', 'Wait', '', '$emp_id' , NOW( )
)";
   
    mysql_query($sql);
    
} else {

    header("location:listCom.php");
}
if (mysql_query) {
// $Updatecnid = mysql_fetch_array(mysql_query('SELECT MAX(alt_Num)+1 FROM altcomservice'));
//        $alt_Num = $Updatecnid[5];
//
//        $sqlUpdatecnid = "UPDATE altcomservice SET alt_Num='$alt_Num' WHERE alt_Id = '$alt_Id'";
//
//        $rsUpdatecnid = mysql_query($sqlUpdatecnid);
        
//    $new_id = mysql_result(mysql_query("Select Max(std_id)+1 as MaxID from  std"),0,"MaxID");
    //  header("location:listCom.php");
    echo "<script language='javascript'>
			   alert('Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
    echo "<meta http-equiv='refresh' content='5; url=listCom.php'>";
} else {
    echo mysql_error();
}
}
?>
